Skip to content

Refactor Proxy.Type to value class for forward compatibility#337

Merged
Goooler merged 5 commits into
trunkfrom
string-proxy-type
Jun 14, 2026
Merged

Refactor Proxy.Type to value class for forward compatibility#337
Goooler merged 5 commits into
trunkfrom
string-proxy-type

Conversation

@Goooler

@Goooler Goooler commented Jun 14, 2026

Copy link
Copy Markdown
Owner

Why we must make this change

  1. Prevent Deserialization Crashes (Forward Compatibility):
    Historically, Proxy.Type was defined as a rigid enum class. Since the native Go layer serializes proxy types as raw string primitives (e.g., "ss", "vmess", "hysteria2"), the app would throw a SerializationException and crash if it encountered a new or custom protocol type added upstream in Mihomo that Kotlin wasn't yet aware of.
  2. Preserve Domain Semantics & Compile-Time Safety:
    While switching to a plain String would avoid deserialization failures, it compromises type safety and code readability. Refactoring Proxy.Type to a @JvmInline value class wrapping a String achieves the best of both worlds:
    • Strong Type Safety: Prevents developers from accidentally passing other string variables (e.g. proxy names or subtitles) as a proxy type.
    • Zero Overhead: Inlined at JVM runtime as a primitive string, meaning zero object allocation overhead and out-of-the-box compatibility with the native wire protocol during JSON serialization/deserialization.
    • Clean Scoping & Maintainability: By keeping Type nested under Proxy and exposing companion constants (like Proxy.Type.Selector and Proxy.Type.URLTest), we minimize code modifications elsewhere and retain autocomplete support.

Ports MetaCubeX@68c4be2.

@Goooler Goooler mentioned this pull request Jun 14, 2026
Comment thread core/src/main/kotlin/com/github/kr328/clash/core/model/Proxy.kt Outdated
@Goooler Goooler changed the title Use JvmInline value class ProxyType for type safety Use value class ProxyType for type safety Jun 14, 2026
@Goooler Goooler changed the title Use value class ProxyType for type safety Use value class Proxy.Type for type safety Jun 14, 2026
@Goooler Goooler changed the title Use value class Proxy.Type for type safety Refactor Proxy.Type to value class for forward compatibility Jun 14, 2026
@Goooler Goooler requested a review from Copilot June 14, 2026 11:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request refactors Proxy.Type from a rigid Kotlin enum class to a @JvmInline value class wrapping a String, improving forward compatibility with upstream proxy/protocol types serialized from the native Go layer without causing SerializationException crashes. To preserve the UI’s ability to distinguish proxy groups from leaf proxies after removing Type.group, it also introduces an explicit isGroup boolean surfaced from the Go layer and consumed by the proxy UI.

Changes:

  • Refactor core.model.Proxy.Type to an inline, serializable, parcelable value class and introduce Proxy.isGroup.
  • Update proxy UI/view-model logic to use proxy.isGroup instead of proxy.type.group.
  • Extend native Go proxy JSON payloads to include isGroup for each proxy entry.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
ui/proxy/src/androidMain/kotlin/com/github/kr328/clash/proxy/vm/ProxyViewModel.kt Switch group-detection logic from type.group to isGroup for linking and display.
ui/proxy/src/androidMain/kotlin/com/github/kr328/clash/proxy/ui/ProxyScreen.kt Update preview data to include isGroup and construct Proxy.Type via string where needed.
core/src/main/kotlin/com/github/kr328/clash/core/model/Proxy.kt Convert Proxy.Type to an inline value class and add isGroup to the model.
core/src/main/golang/native/tunnel/proxies.go Add isGroup to the JSON model and populate it via adapter type assertions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread core/src/main/golang/native/tunnel/proxies.go Outdated
Comment thread core/src/main/golang/native/tunnel/proxies.go Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread core/src/main/kotlin/com/github/kr328/clash/core/model/Proxy.kt
@Goooler Goooler merged commit 1e87dc3 into trunk Jun 14, 2026
5 checks passed
@Goooler Goooler deleted the string-proxy-type branch June 14, 2026 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants